/***************************************************
> Copyright (C) 2024 ==KINGYI== All rights reserved.
> File Name: login_mneu.c
> Author:MOKI
> Mail:1913964829@qq.com 
> Created Time: 2024年09月13日 星期五 18时18分32秒
***************************************************/

#include "global.h"

void isLogReg(int id_flag)
{
    char ch;
    while (1)
    {
        printf("登录或注册（l/r）:");
        setbuf(stdin, NULL); // 清除缓存区
        ch = getchar();
        if (ch == 'l')
        {
            login(id_flag);
            break;
        }
        else if (ch == 'r')
        {
            reg(id_flag);
            break;
        }
        else
        {
            printf("输入有误，请重新输入\n");
        }
    }
}
void login(int id_flag)
{
    char username[50] = {0}, password[20] = {0};
    int close_flag = 0;
    if (id_flag == 1)
    {
        Root user;
        FILE *fp = fopen(ROOTS_FILE_PATH, "r");
        setbuf(stdin, NULL); // 清除缓存区
        printf("请输入用户名：");
        setbuf(stdin, NULL); // 清除缓存区
        scanf("%s", username);
        printf("请输入密码:");
        setbuf(stdin, NULL); // 清除缓存区
        scanf("%s", password);
        // 循环读取存储账号
        size_t rdf = 1;
        while (rdf == 1)
        {
            rdf = fread(&user, sizeof(Root), 1, fp);
            if (strcmp(user.name, username) == 0)
            {
                while (1)
                {
                    if (strcmp(user.password, password) == 0)
                    {
                        printf("登录成功");
                        fclose(fp);
                        close_flag = 1;
                        rootManage();
                        return;
                    }
                    else
                    {
                        printf("密码不正确请重新输入或退出(q)：");
                        setbuf(stdin, NULL); // 清除缓存区
                        scanf("%s", password);
                        if (password == "q")
                        {
                            break;
                        }
                    }
                }
            }
        }
        if (close_flag == 0)
        {
            printf("无此用户");
            fclose(fp);
        }
    }
    else if (id_flag == 2)
    {
        Employee user;
        FILE *fp = fopen(EMPLOYEES_FILE_PATH, "r");
        printf("请输入用户名：");
        setbuf(stdin, NULL); // 清除缓存区
        scanf("%s", username);
        printf("请输入密码:");
        setbuf(stdin, NULL); // 清除缓存区
        scanf("%s", password);
        // 循环读取存储账号
        size_t rdf = 1;
        while (rdf == 1)
        {
            rdf = fread(&user, sizeof(Employee), 1, fp);
            if (strcmp(user.name, username) == 0)
            {
                while (1)
                {
                    if (strcmp(user.password, password) == 0)
                    {
                        printf("登录成功");
                       //strcpy(global_name_employee, user.name);
                        fclose(fp);
                        close_flag = 1;
                        fileWriteGlobal(EMPLOYEE,user.name);
                        employeeManage();
                        return;
                    }
                    else
                    {
                        printf("密码不正确请重新输入或退出(q)：");
                        setbuf(stdin, NULL); // 清除缓存区
                        scanf("%s", password);
                        if (password == "q")
                        {
                            break;
                        }
                    }
                }
            }
        }
        if (close_flag == 0)
        {
            printf("无此用户");
            fclose(fp);
        }
    }
    else if (id_flag == 3)
    {
        Visitor user;
        FILE *fp = fopen(VISITORS_FILE_PATH, "r");
        printf("请输入用户名：");
        setbuf(stdin, NULL); // 清除缓存区
        scanf("%s", username);
        printf("请输入密码:");
        setbuf(stdin, NULL); // 清除缓存区
        scanf("%s", password);
        // 循环读取存储账号
        size_t rdf = 1;
        while (rdf == 1)
        {
            rdf = fread(&user, sizeof(Visitor), 1, fp);
            if (strcmp(user.name, username) == 0)
            {
                while (1)
                {
                    if (strcmp(user.password, password) == 0)
                    {
                        printf("登录成功");
                        fclose(fp);
                        close_flag = 1;
                        fileWriteGlobal(VISITOR,user.name);
                        visitorManage();
                        return;
                    }
                    else
                    {
                        printf("密码不正确请重新输入或退出(q)：");
                        setbuf(stdin, NULL); // 清除缓存区
                        scanf("%s", password);
                        if (password == "q")
                        {
                            break;
                        }
                    }
                }
            }
        }
        if (close_flag == 0)
        {
            printf("无此用户");
            fclose(fp);
        }
    }
}
